package third.lib.zfb.pay.app;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import third.lib.common.exception.ThirdException;
import third.lib.common.logger.ThirdLogger;
import third.lib.common.status.ThirdStatus;
import third.lib.zfb.common.config.ZfbAppConfig;
import third.lib.zfb.common.utils.ZfbUtils;
import third.lib.zfb.pay.app.model.PayNotifyRet;

@Service
public class ZfbAppPay {
    private final Logger logger = LoggerFactory.getLogger(ZfbAppPay.class);
    @Autowired
    ZfbAppConfig config;
    @Autowired
    ThirdLogger thirdLogger;

    public ZfbAppPay() {
    }

    public String getOrderInfo(String amount, String subject, String orderId) throws ThirdException {
        thirdLogger.info("getOrderInfo： " + JSON.toJSONString(this.config));
        AlipayTradeAppPayRequest alipayRequest = new AlipayTradeAppPayRequest();
        alipayRequest.setNotifyUrl(this.config.getNotifyUrl());
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
        model.setOutTradeNo(orderId);
        model.setTotalAmount(amount);
        model.setSubject(subject);
        model.setProductCode("QUICK_MSECURITY_PAY");
        alipayRequest.setBizModel(model);

        AlipayTradeAppPayResponse response;
        try {
            AlipayClient alipayClient = ZfbUtils.getRequest(this.config.getAppId(), this.config.getPriKey(), this.config.getPubAppCrtPath(), this.config.getPubAliCrtPath(), this.config.getPubRootCrtPath());
            response = (AlipayTradeAppPayResponse)alipayClient.sdkExecute(alipayRequest);
        } catch (AlipayApiException var8) {
            var8.printStackTrace();
            throw new ThirdException(ThirdStatus.NET);
        } catch (ThirdException var9) {
            throw var9;
        }

        if (!response.isSuccess()) {
            System.err.println(JSON.toJSONString(response));
            throw new ThirdException(ThirdStatus.ZFB);
        } else {
            return response.getBody();
        }
    }

    public PayNotifyRet verifyPayRet(HttpServletRequest request) throws ThirdException {
        Map<String, String> params = new HashMap();
        Map requestParams = request.getParameterMap();
        Iterator iter = requestParams.keySet().iterator();

        String charset;
        while(iter.hasNext()) {
            charset = (String)iter.next();
            String[] values = (String[])((String[])requestParams.get(charset));
            String valueStr = "";

            for(int i = 0; i < values.length; ++i) {
                valueStr = i == values.length - 1 ? valueStr + values[i] : valueStr + values[i] + ",";
            }

            params.put(charset, valueStr);
        }

        thirdLogger.info(JSON.toJSONString(params));
        PayNotifyRet payRet = new PayNotifyRet();
        charset = (String)params.get("charset");
        String signType = (String)params.get("sign_type");
        boolean bResult = false;

        try {
            bResult = AlipaySignature.rsaCertCheckV1(params, this.config.getPubAliCrtPath(), charset, signType);
        } catch (AlipayApiException var9) {
            var9.printStackTrace();
            throw new ThirdException(ThirdStatus.NET);
        }

        if (!bResult) {
            this.logger.error("签名验证失败: " + JSON.toJSONString(params));
            throw new ThirdException(ThirdStatus.ZFB);
        } else if (!this.config.getAppId().equals(params.get("app_id"))) {
            this.logger.error("app_id验证失败: " + JSON.toJSONString(params));
            throw new ThirdException(ThirdStatus.ZFB);
        } else {
            payRet.setAppId((String)params.get("app_id"));
            payRet.setCharset(charset);
            payRet.setNotifyId((String)params.get("notify_id"));
            payRet.setNotifyTime((String)params.get("notify_time"));
            payRet.setNotifyType((String)params.get("notify_type"));
            payRet.setOutTradeNo((String)params.get("out_trade_no"));
            payRet.setSellerId((String)params.get("seller_id"));
            payRet.setSignType("sign_type");
            payRet.setTotalAmount((String)params.get("total_amount"));
            payRet.setTradeNo((String)params.get("trade_no"));
            payRet.setTradeStatus((String)params.get("trade_status"));
            payRet.setVersion((String)params.get("version"));
            return payRet;
        }
    }
}
